package a08_回溯算法;

import java.util.*;

/**
 * <p>
 * a14_递增子序列复习3
 * </p>
 *
 * @author flyduck
 * @since 2025/4/14
 */
public class a14_递增子序列复习3 {

    public static void main(String[] args) {
        LinkedList<Integer> path = new LinkedList<>();
        path.add(1);
        path.add(2);
        System.out.println(path.peekFirst());//1
        System.out.println(path.peekLast());//2
    }

    public List<List<Integer>> findSubsequences(int[] nums) {
        List<List<Integer>> result = new ArrayList<>();
        LinkedList<Integer> path = new LinkedList<>();
        traversal(result, path, nums, 0);
        return result;
    }

    private void traversal(List<List<Integer>> result,
                           LinkedList<Integer> path,
                           int[] nums,
                           int startIdx){
        if(path.size() >= 2){
            result.add(new ArrayList<>(path));
        }

        Set<Integer> set = new HashSet<>();
        for (int i = startIdx; i < nums.length; i++) {
            if(set.contains(nums[i])){
                continue;
            }
            if(path.size() > 0 && nums[i] < path.peekLast()){
                continue;
            }
            path.add(nums[i]);
            set.add(nums[i]);
            traversal(result, path,nums,i+1);
            path.removeLast();
        }
    }
}
